<template>
  <div class="profile">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
        </div>
      </template>

      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户名">
          {{ userInfo?.username }}
        </el-descriptions-item>
        <el-descriptions-item label="角色">
          {{ userInfo?.role === 'administrator' ? '管理员' : '普通用户' }}
        </el-descriptions-item>
        <el-descriptions-item label="登录时间">
          {{ currentTime }}
        </el-descriptions-item>
        <el-descriptions-item label="Token">
          <el-text truncated>{{ userInfo?.token }}</el-text>
        </el-descriptions-item>
      </el-descriptions>

      <div style="margin-top: 20px;">
        <el-button type="danger" @click="handleLogout">退出登录</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 获取用户信息
const userInfo = computed(() => {
  const userInfoStr = localStorage.getItem('userInfo')
  return userInfoStr ? JSON.parse(userInfoStr) : null
})

// 当前时间
const currentTime = computed(() => {
  return new Date().toLocaleString()
})

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    localStorage.removeItem('userInfo')
    router.push('/login')
    ElMessage.success('已退出登录')
  }).catch(() => {
    // 取消操作
  })
}
</script>

<style scoped>
.profile {
  padding: 20px;
  max-width: 800px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
